import {EntityCollection} from "../entities/basic/EntityCollection";
import {Tank} from "../entities/gameEntity/Tank";
import {Orientation} from "../entities/basic/Orientation";
import {Game} from "./Game";
import {Bullet} from "../entities/gameEntity/Bullet";
import {Position} from "../entities/basic/Position";


export class Drawer {
  game: Game;
  entities: EntityCollection;
  canvas: HTMLCanvasElement;

  context: CanvasRenderingContext2D;

  tankDrawSize: number = Tank.drawSize;
  bulletDrawSize: number = Bullet.drawSize;
  tankImage = new Image();


  constructor(game: Game, canvas: HTMLCanvasElement) {
    this.entities = game.entityCollection;
    this.game = game;
    this.canvas = canvas;

    this.context = this.canvas.getContext("2d")!;



  }

  draw() {


    this.clearAll();
    this.drawEntities();
    this.drawFrameInfos();


  }

  logicFramesPerSecondTemp = 0;
  renderFramesPerSecondTemp = 0
  drawFrameInfos() {

    const currentFrame = this.game.currentFrame;
    let lastFrame = this.game.lastFrameCanRead();
    if (lastFrame == undefined) {
      lastFrame = 0;
    }
    const ctx = this.context;
    ctx.save();
    //每0.5秒更新一次帧率信息
    if(this.game.currentFrame%(this.game.logicFramePerSeconds/2)==0){
      const logicFramesPerSecond = this.game.frameHolder.getLogicFramesPerSecond();
      this.logicFramesPerSecondTemp = (logicFramesPerSecond>this.game.logicFramePerSeconds?this.game.logicFramePerSeconds:logicFramesPerSecond)
      this.logicFramesPerSecondTemp = logicFramesPerSecond
      this.renderFramesPerSecondTemp =this.game.frameHolder.getRenderFramesPerSecond()
    }
    ctx.font = "16px";
    ctx.fillStyle = "blue";
    ctx.fillText("当前帧/最新帧: "+currentFrame + " / " + lastFrame, this.canvas.width - 100, 10, 100);
    ctx.fillStyle = "red";


    ctx.fillText("逻辑帧率: "+this.logicFramesPerSecondTemp, this.canvas.width - 100, 40, 100);

    ctx.fillStyle = "green";

    ctx.fillText("渲染帧率: "+ this.renderFramesPerSecondTemp, this.canvas.width - 100, 70, 100);

    ctx.fillStyle = "red";

    ctx.fillText("ping : "+ this.game.delay, this.canvas.width - 100, 100, 100);
    ctx.restore();
  }

  clearAll() {
    const ctx = this.context;
    ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }

  drawEntities() {
    const ctx = this.context;

    this.entities.getAllEntities().forEach(entity => {
      ctx.save();
      entity.draw(ctx);
      ctx.restore();
    });
  }



  /**
   * 以(x,y)为中心 旋转画布orientation.angle度 设置画布0点为(x,y)
   * @param ctx
   * @param orientation
   * @param x
   * @param y
   */
  static rotateAndTranslate(ctx: CanvasRenderingContext2D, orientation: Orientation, x: number, y: number) {

    ctx.translate(x, y);
    ctx.rotate(orientation.getRadian());

  }



  static defaultEntityDraw(ctx: CanvasRenderingContext2D, image: HTMLImageElement, drawPosition: Position, drawOrientation: Orientation, drawSize: number) {


    ctx.save();

    Drawer.rotateAndTranslate(ctx,drawOrientation, drawPosition.x, drawPosition.y);
    ctx.drawImage(image, -drawSize / 2, -drawSize / 2,drawSize,drawSize);
    ctx.restore();
  }
}
